package com.pocket.demo.business.controller;

import cn.hutool.core.bean.BeanUtil;
import com.pocket.demo.base.api.PageResult;
import com.pocket.demo.base.api.R;
import com.pocket.demo.base.utils.BeanUtils;
import com.pocket.demo.business.controller.vo.HotelPageReqVO;
import com.pocket.demo.business.controller.vo.HotelRespVO;
import com.pocket.demo.business.controller.vo.HotelSaveReqVO;
import com.pocket.demo.business.po.Hotel;
import com.pocket.demo.business.service.HotelService;
import com.pocket.demo.core.i18n.MessageSourceService;
import com.pocket.demo.elasticsearch.doc.HotelDoc;
import com.pocket.demo.elasticsearch.service.HotelDocService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhaozhile
 */
@Tag(name = "酒店模块", description = "酒店管理相关接口")
@Validated
@RestController
@RequestMapping("/hotel")
public class HotelController {

    @Resource
    private MessageSourceService messageSourceService;
    @Resource
    HotelService hotelService;
    @Resource
    HotelDocService hotelDocService;

    @Operation(summary = "新增", description = "新增")
    @PostMapping
    public R<HotelRespVO> add(@RequestBody @Valid HotelSaveReqVO saveReqVO){
        Hotel hotel = hotelService.add(saveReqVO);
        HotelRespVO hotelRespVO = BeanUtil.toBean(hotel, HotelRespVO.class);
        return messageSourceService.getOkResult(hotelRespVO);
    }

    @Operation(summary = "删除", description = "删除")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@Parameter(description = "酒店id") @PathVariable("id") Integer id){
        Integer count = hotelService.delete(id);
        return messageSourceService.getOkResult(count > 0);
    }

    @Operation(summary = "修改", description = "修改")
    @PutMapping
    public R<Boolean> update(@RequestBody @Valid HotelSaveReqVO saveReqVO){
        Boolean result = hotelService.update(saveReqVO);
        return messageSourceService.getOkResult(result);
    }

    @Operation(summary = "单个详情", description = "单个详情")
    @GetMapping("/{id}")
    public R<HotelRespVO> getById(@Parameter(description = "酒店id") @PathVariable("id") Integer id){
        Hotel hotel = hotelService.getById(id);
        HotelRespVO hotelRespVO = BeanUtil.toBean(hotel, HotelRespVO.class);
        return messageSourceService.getOkResult(hotelRespVO);
    }

    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping
    public R<PageResult<HotelRespVO>> list (HotelPageReqVO pageReqVO){
        PageResult<Hotel> result = hotelService.queryByPage(pageReqVO);
        return this.messageSourceService.getOkResult(BeanUtils.toBean(result, HotelRespVO.class));
    }

    @Operation(summary = "ES分页查询", description = "ES分页查询")
    @PostMapping("/search")
    public R<PageResult<SearchHit<HotelDoc>>> search(@RequestBody HotelPageReqVO pageReqVO){
        SearchPage<HotelDoc> searchPage = hotelDocService.searchEs(pageReqVO);
        PageResult<SearchHit<HotelDoc>> pageResult = new PageResult<>(searchPage.getPageable().getPageNumber(), searchPage.getPageable().getPageSize(), searchPage.getTotalElements(), searchPage.getTotalPages(), searchPage.getSearchHits().getSearchHits());
        return this.messageSourceService.getOkResult(pageResult);
    }
}
